package com.whosly.mysql.protocol.code;

import com.google.common.collect.Maps;
import lombok.Getter;

import java.util.Map;

/**
 * @author fengyang
 * @date 2023/8/17 下午8:56
 * @see <a href="yueny09@163.com">mailTo: yueny09@163.com</a>
 */
public enum ErrorCodeType implements IErrorCodeType
{

    MYSQL_PROTOCOL_ERROR(100001, "错误的协议"),

    INVALID_VALUE_TYPE(100007, "错误的数据类型"),

    ;


    /**
     * MYSQL 协议中 errno 必须为int
     */
    @Getter
    private final int code;
    /**
     * MYSQL 协议中 errno 必须为int
     */
    @Getter
    private final String defaultMsg;

    private ErrorCodeType(Integer code) {
        this(code, "");
    }

    private ErrorCodeType(Integer code, String defaultMsg) {
        this.code = code;
        this.defaultMsg = defaultMsg;
    }

    public String getName() {
        return this.name();
    }

    @Override
    public String getDomain()
    {
        return null;
    }

    private final static Map<Integer, ErrorCodeType> protoType = Maps.newHashMap();
    static {
        for (ErrorCodeType errorCode : ErrorCodeType.values()) {
            protoType.put(errorCode.getCode(), errorCode);
        }
    }

    public static ErrorCodeType valueOf(Integer code) {
        ErrorCodeType errorCode = protoType.get(code.intValue());
        if (errorCode == null) {
            return null;
        } else {
            return errorCode;
        }
    }

}
